package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserCharitable;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/9/1
 */
public class UserCharitableDaoImpl extends TempDataObjectDao<UserCharitable> {

    public static String getUserCharitableSql = "select * from t_u_charitable where userId=?";

    public UserCharitable getUserCharitable(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserCharitableSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCharitable userCharitable = parse(rs);
                return userCharitable;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }


    public static String insertUserCharitableSql = "insert into t_u_charitable(userId,resetTime,todayCharitableTimes,charitableType,inSystem,allCostMoney) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserCharitable userCharitable) {
        userCharitable.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCharitable.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userCharitable.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitable.getTodayCharitableTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitable.getCharitableType()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userCharitable.isInSystem()));
        para.put(i++, new DbParameter(Types.VARCHAR, userCharitable.getAllCostMoney().toString()));
        if (getDb().executeNoneQuery(insertUserCharitableSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateUserCharitableSql = "update t_u_charitable set resetTime=?,todayCharitableTimes=?,charitableType=?,inSystem=?,allCostMoney=? where userId=?";

    @Override
    protected boolean updateData(UserCharitable userCharitable) {
        userCharitable.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCharitable.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitable.getTodayCharitableTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitable.getCharitableType()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userCharitable.isInSystem()));
        para.put(i++, new DbParameter(Types.VARCHAR, userCharitable.getAllCostMoney().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, userCharitable.getUserId()));
        if (getDb().executeNoneQuery(updateUserCharitableSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserCharitable parseData(ResultSet rs) throws SQLException {
        UserCharitable userCharitable = new UserCharitable();
        userCharitable.setUserId(rs.getLong("userId"));
        userCharitable.setResetTime(rs.getLong("resetTime"));
        userCharitable.setTodayCharitableTimes(rs.getInt("todayCharitableTimes"));
        userCharitable.setCharitableType(rs.getInt("charitableType"));
        userCharitable.setInSystem(rs.getBoolean("inSystem"));
        userCharitable.setAllCostMoney(new BigInteger(rs.getString("allCostMoney")));
        return userCharitable;
    }
}
